package leetcode;

import java.util.Stack;

public class L20 {

        public boolean isValid(String s) {
            Stack<Character> stack=new Stack<>();
            for(char c:s.toCharArray()){
                if(c=='{'||c=='('||c=='['){
                    stack.push(c);
                }
                else
                {
                    if(stack.isEmpty()){
                        return false;
                    }
                    char o=stack.pop();
                    if(c=='}'&&o!='{'||c==']'&&o!='['||c==')'&&o!='('){
                        return false;
                    }

                }
            }
            return stack.isEmpty();
        }

}
